package com.topvision.jack.uploadlibrary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.topvision.jack.uploadlibrary.greendao.DaoMaster;
import com.topvision.jack.uploadlibrary.greendao.DaoSession;
import com.topvision.jack.uploadlibrary.greendao.UploadFileEntityDao;


/**
 * Created by Administrator on 2017/11/29.
 */

public class DbManager {
    private final static String dbName = "video.db";
    private static DbManager instance;
    private DaoMaster.DevOpenHelper openHelper;
    Context context;
    UploadFileEntityDao uploadFileEntityDao;

    private DbManager(Context context) {
        this.context = context;
    }

    public static DbManager getInstance(Context context) {
        if (instance == null) {
            synchronized (DbManager.class) {
                if (instance == null) {
                    instance = new DbManager(context);
                }
            }
        }
        return instance;
    }



    public UploadFileEntityDao getUploadFileEntityDao() {
        if (uploadFileEntityDao == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
            DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
            DaoSession daoSession = daoMaster.newSession();
            uploadFileEntityDao = daoSession.getUploadFileEntityDao();
        }
        return uploadFileEntityDao;
    }


    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

}
